<template>
  <div class="pagination">
    <button
      :disabled="currentPage === 1"
      @click="currentPage = currentPage - 1"
    >
      上一页
    </button>
    <button v-show="startEnd.start > 1" @click="currentPage = 1">1</button>
    <button v-show="startEnd.start > 2" disabled>···</button>

    <button
      v-for="page in pages"
      :key="page.num"
      :class="{ active: page.num === currentPage }"
      @click="currentPage = page.num"
    >
      {{ +page.num }}
    </button>

    <button v-show="startEnd.end < totalPages - 1" disabled>···</button>
    <button
      v-show="startEnd.end < totalPages"
      @click="currentPage = totalPages"
    >
      {{ totalPages }}
    </button>
    <button :disabled="currentPage === totalPages" @click="currentPage + 1">
      下一页
    </button>

    <button style="margin-left: 30px">共 {{ totalSize }} 条</button>
  </div>
</template>

<script>
/*
外部传入数据:
    totalSize   总条数
    pageSize    每页几条
    continuousPage  连续页码
    outPage     外部传入当前页

内部:
    totalPages  总页数
    currentPage 当前页
    start、end 连续页码的 开始页、结束页
    
*/
export default {
  name: "pagination",
  props: ["totalSize", "pageSize", "continuousPage", "initPage"],
  data() {
    return {
      currentPage: this.initPage,
    };
  },
  computed: {
    // 总页数
    totalPages() {
      return Math.ceil(this.totalSize / this.pageSize);
    },
    startEnd() {
      // 关系
      // end-start=continuousPage-1
      // end = continuousPage+start-1
      // start = end-continuousPage+1
      let continuousPage = +this.continuousPage
      let start = 0;
      let end = 0;
      start = this.currentPage - Math.floor(continuousPage / 2);
      // 判断开始页是否合法
      if (start < 1) {
        start = 1;
      }
      // end补位
      // console.log(start);
      end = continuousPage + start - 1;
      // 判断结束页是否合法
      if (end > this.totalPages) {
        end = this.totalPages;
        // start补位
        start = end - continuousPage + 1;
        // console.log(start);
        // 结束页和开始页都不合法
        if (start < 1) {
          start = 1;
        }
      }
      return { start, end };
    },
    // 连续页数组
    pages(){
        let arr = [];
        for (let i = 1; i <= this.totalPages; i++) {
          arr.push({ num: i });
        }
        let pages = arr.filter(
          (item) =>
            (item.num >= this.startEnd.start && item.num <= this.startEnd.end)
        );
        return pages;
    },
  },
  watch: {
    // 当前页变化，触发外部页面
    currentPage: {
      handler(newPage) {
        this.$bus.$emit("pageChange",newPage)
      },
    },
    // 外部重选参数，当前页返回第一页
    initPage: {
      handler(newInitpage) {
        this.currentPage = newInitpage;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.pagination {
  text-align: center;
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
}
</style>
